package com.example.edianzu.somur.module.mine;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.example.edianzu.somur.MainActivity;
import com.example.edianzu.somur.R;
import com.example.edianzu.somur.api.APIRequest;
import com.example.edianzu.somur.api.bean.LoginInfo;
import com.example.edianzu.somur.module.mine.activity.AddSampleActivity;
import com.example.edianzu.somur.module.mine.activity.PersonalCenterActivity;
import com.example.edianzu.somur.module.mine.activity.ReportActivity;
import com.example.edianzu.somur.utils.view.BaseFragment;
import com.example.edianzu.somur.utils.view.GlideCircleTransform;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment {

    @BindView(R.id.fragment_mine_user)
    AppCompatImageView fragmentMineUser;
    @BindView(R.id.fragment_mine_in)
    AppCompatImageView fragmentMineIn;
    @BindView(R.id.fragment_mine_DNA)
    AppCompatImageView fragmentMineDNA;
    @BindView(R.id.fragment_mine_intestine)
    AppCompatImageView fragmentMineIntestine;
    @BindView(R.id.fragment_mine_addSample)
    AppCompatButton fragmentMineAddSample;
    Unbinder unbinder;
    @BindView(R.id.fragment_mine_RelativeLayout)
    RelativeLayout fragmentMineRelativeLayout;
    @BindView(R.id.fragment_mine_userName)
    AppCompatTextView fragmentMineUserName;
    private LoginInfo loginInfo;

    public MineFragment() {
        // Required empty public constructor
    }


    @Override
    protected void initViewPagerFragment(List<String> title, List<Fragment> fragmentList) {

    }

    @Override
    protected boolean isLoadTopView() {
        return false;
    }

    @Override
    protected int setTopViewId() {
        return 0;
    }

    @Override
    protected int setLoadBottomViewId() {
        return 0;
    }

    @Override
    protected boolean isLoadBottomView() {
        return false;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);

        Glide.with(getActivity()).load(loginInfo!=null?loginInfo.getData().getIcon():"null").transform(new GlideCircleTransform(getActivity())).into(fragmentMineUser);
        fragmentMineUserName.setText(loginInfo!=null?loginInfo.getData().getName():"name");

//        Log.d("MineFragment", "onCreateView: " + loginInfo.getData().getName() + "-----" + loginInfo.getData().getSex() + "---------" + loginInfo.getData().getBirthday()
//                + loginInfo.getData().getMobile());
        return view;
    }

    //Fragment中的onAttach方法
    //通过强转成宿主activity，就可以获取到传递过来 的数据
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        loginInfo = ((MainActivity) activity).getLoginInfo();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.fragment_mine_RelativeLayout, R.id.fragment_mine_user, R.id.fragment_mine_in, R.id.fragment_mine_DNA, R.id.fragment_mine_intestine, R.id.fragment_mine_addSample})
    public void onViewClicked(View view) {

        Intent intent = new Intent();
        switch (view.getId()) {

            case R.id.fragment_mine_RelativeLayout:

                if (loginInfo != null) {

                    intent.setClass(getContext(), PersonalCenterActivity.class);
                    intent.putExtra("loginInfo", loginInfo);
                    startActivity(intent);
                }
                break;

            case R.id.fragment_mine_user:

                if (loginInfo != null) {

                    intent.setClass(getContext(), PersonalCenterActivity.class);
                    intent.putExtra("loginInfo", loginInfo);
                    startActivity(intent);
                }
                break;
            case R.id.fragment_mine_in:

                if (loginInfo != null) {

                    intent.setClass(getContext(), PersonalCenterActivity.class);
                    intent.putExtra("loginInfo", loginInfo);
                    startActivity(intent);
                }
                break;
            case R.id.fragment_mine_DNA:

                /*type: 肠道 7，基因报告1   必填
                open_id：用户open_id
                company_id :公司id*/

                intent.setClass(getContext(), ReportActivity.class);
                intent.putExtra("url", APIRequest.REPORT_URL + "open_id=" + loginInfo.getData().getOpen_id() + "&company_id="
                        + loginInfo.getData().getCompany_id() + "&type=1");
                startActivity(intent);
                break;
            case R.id.fragment_mine_intestine:

                intent.setClass(getContext(), ReportActivity.class);
                intent.putExtra("url", APIRequest.REPORT_URL + "open_id=" + loginInfo.getData().getOpen_id() + "&company_id="
                        + loginInfo.getData().getCompany_id() + "&type=7");
                startActivity(intent);
                break;
            case R.id.fragment_mine_addSample:

                intent.setClass(getContext(), AddSampleActivity.class);
                intent.putExtra("member_id", loginInfo.getData().getMember_id());
                startActivity(intent);
                break;
        }
    }
}
